import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:talk/utils/toast_utils.dart';
import 'package:flutter/services.dart'; // 导入Clipboard所需库

import '../../api/DeApi.dart';
import '../../api/DeviceApi.dart';
import '../../commponets/CustomDialog.dart';
import '../../commponets/ItemView.dart';
import '../../home/AppLocale.dart';
import '../../tools/widgets/my_app_bar.dart';
import '../../tools/widgets/my_button.dart';
import 'model/Device.dart';

class DeviceInfoPage extends StatefulWidget {
  const DeviceInfoPage({
    super.key,
    this.deviceId,
    this.device,
  });

  final int? deviceId;
  final Device? device;

  @override
  State<DeviceInfoPage> createState() => _DeviceInfoPageState();
}

class _DeviceInfoPageState extends State<DeviceInfoPage> {
  final DeviceApi _deviceApi = DeviceApi();
  late int _deviceId;
  Device? _device;

  @override
  void initState() {
    super.initState();

    _deviceId = widget.deviceId!;
    _device = widget.device!;
    // _fetchData();
  }



  // Future<void> _fetchData() async {
  //   try {
  //     final response = await _deviceApi.getDeviceInfo(_deviceId);
  //
  //     if (response.code == 200) {
  //
  //       setState(() {
  //         _device = Device.fromJson(response.data);
  //       });
  //
  //
  //     } else {
  //       AppLocale.text('s_500');
  //     }
  //   } catch (e) {
  //     AppLocale.text('s_get500');;
  //   }
  // }



  Future<void> _copyToClipboard() async {
    if (_device?.deviceUuid == null) {
      Toast.show("UUID为空，无法复制");
      return;
    }

    try {
      // 使用Clipboard.setData方法复制文本
      await Clipboard.setData(
        ClipboardData(text: _device!.deviceUuid!),
      );
      Toast.show("UUID已复制到剪贴板");
    } catch (e) {
      Toast.show("复制失败：$e");
    }
  }





  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: AppLocale.text('d_shebeixinxi'),
        backgroundColor: Colors.transparent,
        dropdownWidth: 140,
        dropdownBackgroundColor: Colors.white,
        dropdownBorderRadius: 20,
      ),


      body: Container(
          child: Column(
            children: [

              const SizedBox(height: 20,),


              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_shebeixinghao'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.deviceSn ?? '',
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),



              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_hujiaodizhi'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.deviceModel ?? '',
                      style: TextStyle(
                        fontSize: 14.0,
                        color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),


              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    const Text(
                      "UUID",
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Row(
                      children: [
                        Text(
                          _device?.deviceUuid ?? '',
                          style: TextStyle(
                              fontSize: 14.0,
                              color: Colors.black54
                          ),
                        ),
                        Image.asset("images/images/deviceSetting/i1.png", width: 24),
                      ],
                    )
                  ],
                ),
                borderRadius: 8,
                onTap: () {
                  _copyToClipboard(); // 点击时调用复制方法
                },
              ),




              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_ipdizhi'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.ipAddress ?? '',
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),

              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_macdizhi'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.macAddress ?? '',
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),


              const SizedBox(height: 10,),

              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_shebbeierweima'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return CustomAlertDialog(
                        title: 'UUID:${_device?.deviceUuid ?? ''}',
                        titleVerticalPadding: 24,
                        titleHorizontalPadding: 2,
                        titleStyle: TextStyle(
                          fontSize: 15.0, // 设置标题大小
                          color: Colors.black, // 设置标题颜色
                          // fontWeight: FontWeight.bold,
                        ),
                        contentTopPadding: 1.0, // 设置内容与标题的间距
                        content: Image.network(
                          _device?.devicePho ?? '',
                          width: 170, // 设置图片宽度
                          height: 170, // 设置图片高度
                          fit: BoxFit.contain, // 设置图片适应方式
                        ),
                        contentAlignment: Alignment.topCenter, // 图片置顶对齐
                        // contentConstraints: BoxConstraints(
                        //   maxWidth: 240, // 内容最大宽度
                        //   maxHeight: 240, // 内容最大高度
                        // ),
                        // showConfirmButton: true,
                        // onConfirm: () => Navigator.of(context).pop(),
                      );
                    },
                  );
                },
              ),



              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_shebbeiwangluo'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.network ?? '',
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),

              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_cunchukongjian'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.storageRemaining == null && _device?.storageTotal == null
                          ? AppLocale.text('d_kong')
                          : '${(_device?.storageRemaining ?? 0.0)}/${(_device?.storageTotal ?? 0.0)}G' + AppLocale.text('d_shengyukongjian'),
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54),
                    )
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),


              const SizedBox(height: 10,),


              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_yunxingshijian'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.runTime ?? '',
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),
              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_zaixianshijian'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Text(
                      _device?.lineTime ?? '',
                      style: TextStyle(
                          fontSize: 14.0,
                          color: Colors.black54
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),




            ],
          )
      ),
    );
  }



}



















